<template>
  <a-modal
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    width="850px"
    :confirmLoading="confirmLoading"
    v-drag
  >
    <template slot="footer">
      <a-button @click="handleCancel" :disabled="confirmLoading">{{ $t('关闭') }}</a-button>
      <a-button type="primary" @click="handleOk" :disabled="confirmLoading">{{ $t('保存') }}</a-button>
    </template>
    <div style="height: 500px; overflow-y: auto">
      <a-form-model ref="dataForm" :rules="rules" :model="dataForm" v-enterToNext>
        <a-form-model-item
          prop="allowMemberType"
          :labelCol="{ span: 2 }"
          :wrapperCol="{ span: 20 }"
          style="margin-bottom: 5px; margin-left: 4px"
          :label="$t('适用会员')"
        >
          <a-radio-group v-model="allowMemberType" @change="getTypeValue" style="margin-left: 5px">
            <a-radio value="">{{ $t('全部') }}</a-radio>
            <a-radio value="part">{{ $t('指定会员') }}</a-radio> </a-radio-group
          ><br />
          <a-checkbox-group
            :span="24"
            v-if="memberShow"
            v-model="dataForm.allowMemberTypes"
            style="margin-top: 6px; margin-left: 5px"
            @change="changeType"
          >
            <a-checkbox style="margin-left: 8px" :value="item.id" v-for="item in memberList" :key="item.id">{{
              item.name
            }}</a-checkbox>
          </a-checkbox-group>
        </a-form-model-item>
      </a-form-model>
    </div>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
let that
export default {
  name: 'ApplySettingModal',
  components: {},
  data() {
    return {
      form: this.$form.createForm(this),
      title: this.$t('项目应用限制'),
      confirmLoading: false,
      dataForm: {},
      memberShow: false,
      visible: false,
      rules: {},
      memberList: [],
      allowMemberType: '',
      secMembers: [],
    }
  },
  mounted() {
    that = this
  },
  methods: {
    show(record) {
      that.allowMemberType = ''
      that.memberShow = false
      that.secMembers = []
      that.visible = true
      that.dataForm = Object.assign({}, { allowMemberTypes: [] }, record)
      that.getMemberList()
    },
    getTypeValue(e) {
      if (e.target.value == 'part') {
        that.memberShow = true
      } else {
        that.memberShow = false
        that.dataForm.allowMemberTypes = []
      }
    },
    getMemberList() {
      getAction('/member/bisMemberRule/list', { notNonmember: 1 }).then((res) => {
        if (res.success) {
          that.memberList = res.result.records
        }
      })
      getAction('/game/gameApply/getByGameId', { gameId: that.dataForm.id }).then((res) => {
        if (res.success) {
          that.secMembers = res.result.memberList
          if (res.result.memberList.length > 0) {
            that.allowMemberType = 'part'
            that.memberShow = true
            that.dataForm.allowMemberTypes = res.result.memberList.map((m) => {
              return m.memberType
            })
          }
        }
      })
    },
    close() {
      that.$emit('close')
      that.visible = false
    },
    handleOk() {
      let querydata = {}
      that.$refs.dataForm.validate((valid) => {
        if (valid) {
          querydata.memberList = []
          if (that.allowMemberType == 'part' && that.dataForm.allowMemberTypes.length == 0) {
            that.$message.warn(that.$t('请选择会员类型'))
            return
          }
          that.dataForm.allowMemberTypes.forEach((mt) => {
            let x = that.secMembers.findIndex((s) => {
              return s.memberType == mt
            })
            if (x > -1) {
              querydata.memberList.push(that.secMembers[x])
            } else {
              querydata.memberList.push({ memberType: mt })
            }
          })
          querydata.gameId = that.dataForm.id
          let httpurl = '/game/gameApply/edit'
          let method = 'put'
          that.confirmLoading = true
          httpAction(httpurl, querydata, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    handleCancel() {
      that.close()
    },
    changeType(e){
      console.log(e)
    }
  },
}
</script>
<style scoped>
</style>